package com.hrma.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrma.dao.RecruitScheduleMapper;
import com.hrma.dao.TalentPoolMapper;
import com.hrma.entity.SelectResult;
import com.hrma.entity.TalentPool;
import com.hrma.entity.TalentPoolDTO;
import com.hrma.entity.TalentPoolVO;
import com.hrma.entity.exception.StateException;
import com.hrma.services.TalentPoolService;
import com.hrma.utils.UserUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TalentPoolImpl implements TalentPoolService {
    private final TalentPoolMapper talentPoolMapper;

    public TalentPoolImpl(TalentPoolMapper talentPoolMapper, RecruitScheduleMapper recruitScheduleMapper) {
        this.talentPoolMapper = talentPoolMapper;
        this.recruitScheduleMapper = recruitScheduleMapper;
    }

    private final RecruitScheduleMapper recruitScheduleMapper;

    @Override
    public TalentPool add(TalentPool data) {
        if (!talentPoolMapper.exist(data.getId())) {
            throw new RuntimeException("id字段不存在！");
        }
        talentPoolMapper.add(data);
        return data;
    }

    @Override
    public void delete(int id) {
        if (!talentPoolMapper.exist(id)) {
            throw new RuntimeException("id字段不存在！");
        }
        if((talentPoolMapper.getstate(id) == 1) || (talentPoolMapper.getstate(id) == 2) || (talentPoolMapper.getstate(id) == 3)){
            throw new StateException("人才当前正在招聘中！");
        }
        talentPoolMapper.delete(id);
    }

    @Override
    public void update(TalentPool data) {
        if (!talentPoolMapper.exist(data.getId())) {
            throw new RuntimeException("id字段不存在！");
        }
        talentPoolMapper.update(data);
    }

    @Override
    public List<SelectResult> select() {
        return talentPoolMapper.select();
    }

    @Override
    public Page<TalentPoolVO> getList(TalentPoolDTO data) {
        PageHelper.startPage(data.getPageNum(),data.getPageSize());
        return talentPoolMapper.getList(data);
    }

    @Override
    public void recruit(TalentPoolVO data) {
        if((talentPoolMapper.getstate(data.getId()) != 0) || (talentPoolMapper.getstate(data.getId()) != 5) || (talentPoolMapper.getstate(data.getId()) != 6)){
            throw new StateException("已招聘！");
        }
        talentPoolMapper.setState(data.getState(),data.getId());
        recruitScheduleMapper.add(data.getId(), UserUtil.getUserId());
    }
}
